
clear
set more off
program drop _all

use "INTERMEDIATE/firm_RMS_def10_sampleE_06_15.dta", clear
gen censored_gr =.
replace censored_gr = 1 if lcensored_i==0 & rcensored_i==0
replace censored_gr = 3 if lcensored_i==0 & rcensored_i==1
replace censored_gr = 2 if lcensored_i==1 & rcensored_i==0
replace censored_gr = 4 if lcensored_i==1 & rcensored_i==1
tab quarter censored_gr
////////////
bys firm: egen revenue_r_sum=sum(rev_r_it)
bys firm: egen revenue_r_m=mean(rev_r_it) if age_i>=1 & age<=maxAge_i
bys firm: egen products_r_sum=sum(T2_it)
bys firm: egen products_r_m=mean(T2_it) if age_i>=1 & age<=maxAge_i
bys firm: egen groups_r_sum=sum(groups_it)
bys firm: egen groups_r_m=mean(groups_it) if age_i>=1 & age<=maxAge_i
bys firm: egen quality0_m=mean(q_it)
keep if age_i>=1 & age<=maxAge_i
keep firm censored_gr censored revenue_r_sum revenue_r_m products_r_sum products_r_m groups_r_sum groups_r_m maxAge_i quality0_m
duplicates drop
duplicates drop firm, force  

gen total_prods=1
gen total_prods4=1 if maxAge<=4
gen total_prods16=1 if maxAge<=16
gen total_prods16p=1 if maxAge>16
gen total_prods28p=1 if maxAge>28
egen x=sum(revenue_r_sum)
egen y=sum(total_prods)
gen total_prods_w=revenue_r_sum/x
gen total_prods_s=total_prods/y

collapse (sum) total_prods total_prods_s total_prods_w revenue_r_sum total_prods4 total_prods16 total_prods16p total_prods28p ///
(mean) revenue_r_m_mean=revenue_r_m products_r_m_mean=products_r_m groups_r_m_mean=groups_r_m maxAge quality0_m_mean=quality0_m  ///
(median) revenue_r_m_median=revenue_r_m products_r_m_median=products_r_m groups_r_m_median=groups_r_m maxAge_median=maxAge quality0_m_median=quality0_m ///
(p25) revenue_r_m_p25=revenue_r_m products_r_m_p25=products_r_m groups_r_m_p25=groups_r_m maxAge_p25=maxAge quality0_m_p25=quality0_m ///
(p75) revenue_r_m_p75=revenue_r_m products_r_m_p75=products_r_m groups_r_m_p75=groups_r_m maxAge_p75=maxAge quality0_m_p75=quality0_m ///
(p90) revenue_r_m_p90=revenue_r_m products_r_m_p90=products_r_m groups_r_m_p90=groups_r_m maxAge_p90=maxAge quality0_m_p90=quality0_m ///
(p95) revenue_r_m_p95=revenue_r_m products_r_m_p95=products_r_m groups_r_m_p95=groups_r_m maxAge_p95=maxAge quality0_m_p95=quality0_m, by (censored_gr)

replace maxAge_i = round(maxAge_i+1, 0.1)
replace  total_prods4= round(total_prods4/total_prods*100, 0.1)
replace  total_prods16= round(total_prods16/total_prods*100, 0.1)
replace  total_prods16p= round(total_prods16p/total_prods*100, 0.1)
replace  total_prods28p= round(total_prods28p/total_prods*100, 0.1)

replace  revenue_r_m_mean=round(revenue_r_m_mean/1000,0.1)
replace  revenue_r_m_p25= round(revenue_r_m_p25/1000, 0.1)
replace  revenue_r_m_median= round(revenue_r_m_median/1000, 0.1)
replace  revenue_r_m_p75= round(revenue_r_m_p75/1000, 0.1)
replace  revenue_r_m_p90= round(revenue_r_m_p90/1000, 0.1)
replace  revenue_r_m_p95= round(revenue_r_m_p95/1000, 0.1)

replace  products_r_m_mean=round(products_r_m_mean,0.1)
replace  products_r_m_p25= round(products_r_m_p25, 0.1)
replace  products_r_m_median= round(products_r_m_median, 0.1)
replace  products_r_m_p75= round(products_r_m_p75, 0.1)
replace  products_r_m_p90= round(products_r_m_p90, 0.1)
replace  products_r_m_p95= round(products_r_m_p95, 0.1)

replace  groups_r_m_mean=round(groups_r_m_mean,0.1)
replace  groups_r_m_p25= round(groups_r_m_p25, 0.1)
replace  groups_r_m_median= round(groups_r_m_median, 0.1)
replace  groups_r_m_p75= round(groups_r_m_p75, 0.1)
replace  groups_r_m_p90= round(groups_r_m_p90, 0.1)
replace  groups_r_m_p95= round(groups_r_m_p95, 0.1)

label define censored_gr_l 1 "Not Censored" 2 "Right Censored" 3 "Left Censored" 4 "Right & Left Censored"
label values censored_gr censored_gr_l

label variable total_prods "Total \# of firms"
label variable total_prods_s "Share"
label variable total_prods_w "Share (rev. weighted)"

label variable maxAge_i "   average"
label variable total_prods4 "   less than 4"
label variable total_prods16 "   less than 16"
label variable total_prods28p "   above 28"

label variable revenue_r_m_mean "mean"
label variable revenue_r_m_p25 "25th percentile"
label variable revenue_r_m_median "median"
label variable revenue_r_m_p75 "75th percentile"
label variable revenue_r_m_p90 "90th percentile"
label variable revenue_r_m_p95 "95th percentile"

label variable products_r_m_mean " mean"
label variable products_r_m_p25 "25th percentile"
label variable products_r_m_median "median"
label variable products_r_m_p75 "75th percentile"
label variable products_r_m_p90 "90th percentile"
label variable products_r_m_p95 "95th percentile"

label variable groups_r_m_mean " mean"
label variable groups_r_m_p25 "25th percentile"
label variable groups_r_m_median "median"
label variable groups_r_m_p75 "75th percentile"
label variable groups_r_m_p90 "90th percentile"
label variable groups_r_m_p95 "95th percentile"

label variable quality0_m_mean "mean"
label variable quality0_m_p25 "25th percentile"
label variable quality0_m_median "median"
label variable quality0_m_p75 "75th percentile"
label variable quality0_m_p90 "90th percentile"
label variable quality0_m_p95 "95th percentile"

preserve
estpost tabstat total_prods maxAge_i total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 products_r_m_mean products_r_m_p25 products_r_m_median products_r_m_p75 products_r_m_p90 products_r_m_p95 groups_r_m_mean groups_r_m_p25 groups_r_m_median groups_r_m_p75 groups_r_m_p90 groups_r_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo firm1_v1
restore

estpost tabstat total_prods maxAge_i total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 products_r_m_mean products_r_m_p25 products_r_m_median products_r_m_p75 products_r_m_p90 products_r_m_p95 groups_r_m_mean groups_r_m_p25 groups_r_m_median groups_r_m_p75 groups_r_m_p90 groups_r_m_p95 quality0_m_mean quality0_m_p25 quality0_m_median quality0_m_p75 quality0_m_p90 quality0_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo firm1_v2

use "INTERMEDIATE/firm_RMS_def10_sampleE_06_15.dta", clear
gen censored_gr =5

bys firm: egen revenue_r_sum=sum(rev_r_it)
bys firm: egen revenue_r_m=mean(rev_r_it) if age_i>=1 & age<=maxAge_i
bys firm: egen products_r_sum=sum(T2_it)
bys firm: egen products_r_m=mean(T2_it) if age_i>=1 & age<=maxAge_i
bys firm: egen groups_r_sum=sum(groups_it)
bys firm: egen groups_r_m=mean(groups_it) if age_i>=1 & age<=maxAge_i
bys firm: egen quality0_m=mean(q_it)
keep if age_i>=1 & age<=maxAge_i
keep firm censored_gr censored revenue_r_sum revenue_r_m products_r_sum products_r_m groups_r_sum groups_r_m maxAge_i quality0_m
duplicates drop
duplicates drop firm, force   

gen total_prods=1
gen total_prods4=1 if maxAge<=4
gen total_prods16=1 if maxAge<=16
gen total_prods16p=1 if maxAge>16
gen total_prods28p=1 if maxAge>28
egen x=sum(revenue_r_sum)
egen y=sum(total_prods)
gen total_prods_w=revenue_r_sum/x
gen total_prods_s=total_prods/y

collapse (sum) total_prods total_prods_s total_prods_w revenue_r_sum total_prods4 total_prods16 total_prods16p total_prods28p ///
(mean) revenue_r_m_mean=revenue_r_m products_r_m_mean=products_r_m groups_r_m_mean=groups_r_m maxAge quality0_m_mean=quality0_m  ///
(median) revenue_r_m_median=revenue_r_m products_r_m_median=products_r_m groups_r_m_median=groups_r_m maxAge_median=maxAge quality0_m_median=quality0_m ///
(p25) revenue_r_m_p25=revenue_r_m products_r_m_p25=products_r_m groups_r_m_p25=groups_r_m maxAge_p25=maxAge quality0_m_p25=quality0_m ///
(p75) revenue_r_m_p75=revenue_r_m products_r_m_p75=products_r_m groups_r_m_p75=groups_r_m maxAge_p75=maxAge quality0_m_p75=quality0_m ///
(p90) revenue_r_m_p90=revenue_r_m products_r_m_p90=products_r_m groups_r_m_p90=groups_r_m maxAge_p90=maxAge quality0_m_p90=quality0_m ///
(p95) revenue_r_m_p95=revenue_r_m products_r_m_p95=products_r_m groups_r_m_p95=groups_r_m maxAge_p95=maxAge quality0_m_p95=quality0_m, by (censored_gr)

replace maxAge_i = round(maxAge_i+1, 0.1)
replace  total_prods4= round(total_prods4/total_prods*100, 0.1)
replace  total_prods16= round(total_prods16/total_prods*100, 0.1)
replace  total_prods16p= round(total_prods16p/total_prods*100, 0.1)
replace  total_prods28p= round(total_prods28p/total_prods*100, 0.1)

replace  revenue_r_m_mean=round(revenue_r_m_mean/1000,0.1)
replace  revenue_r_m_p25= round(revenue_r_m_p25/1000, 0.1)
replace  revenue_r_m_median= round(revenue_r_m_median/1000, 0.1)
replace  revenue_r_m_p75= round(revenue_r_m_p75/1000, 0.1)
replace  revenue_r_m_p90= round(revenue_r_m_p90/1000, 0.1)
replace  revenue_r_m_p95= round(revenue_r_m_p95/1000, 0.1)

replace  products_r_m_mean=round(products_r_m_mean,0.1)
replace  products_r_m_p25= round(products_r_m_p25, 0.1)
replace  products_r_m_median= round(products_r_m_median, 0.1)
replace  products_r_m_p75= round(products_r_m_p75, 0.1)
replace  products_r_m_p90= round(products_r_m_p90, 0.1)
replace  products_r_m_p95= round(products_r_m_p95, 0.1)

replace  groups_r_m_mean=round(groups_r_m_mean,0.1)
replace  groups_r_m_p25= round(groups_r_m_p25, 0.1)
replace  groups_r_m_median= round(groups_r_m_median, 0.1)
replace  groups_r_m_p75= round(groups_r_m_p75, 0.1)
replace  groups_r_m_p90= round(groups_r_m_p90, 0.1)
replace  groups_r_m_p95= round(groups_r_m_p95, 0.1)

label define censored_gr_l 5 "All"
label values censored_gr censored_gr_l

label variable total_prods "Total \# of firms"
label variable total_prods_s "Share"
label variable total_prods_w "Share (rev. weighted)"

label variable maxAge_i "   average"
label variable total_prods4 "   less than 4"
label variable total_prods16 "   less than 16"
label variable total_prods28p "   above 28"

label variable revenue_r_m_mean "mean"
label variable revenue_r_m_p25 "25th percentile"
label variable revenue_r_m_median "median"
label variable revenue_r_m_p75 "75th percentile"
label variable revenue_r_m_p90 "90th percentile"
label variable revenue_r_m_p95 "95th percentile"

label variable products_r_m_mean " mean"
label variable products_r_m_p25 "25th percentile"
label variable products_r_m_median "median"
label variable products_r_m_p75 "75th percentile"
label variable products_r_m_p90 "90th percentile"
label variable products_r_m_p95 "95th percentile"

label variable groups_r_m_mean " mean"
label variable groups_r_m_p25 "25th percentile"
label variable groups_r_m_median "median"
label variable groups_r_m_p75 "75th percentile"
label variable groups_r_m_p90 "90th percentile"
label variable groups_r_m_p95 "95th percentile"

label variable quality0_m_mean "mean"
label variable quality0_m_p25 "25th percentile"
label variable quality0_m_median "median"
label variable quality0_m_p75 "75th percentile"
label variable quality0_m_p90 "90th percentile"
label variable quality0_m_p95 "95th percentile"

preserve
estpost tabstat total_prods maxAge_i total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 products_r_m_mean products_r_m_p25 products_r_m_median products_r_m_p75 products_r_m_p90 products_r_m_p95 groups_r_m_mean groups_r_m_p25 groups_r_m_median groups_r_m_p75 groups_r_m_p90 groups_r_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo firm2_v1
restore

estpost tabstat total_prods maxAge_i total_prods4 total_prods16 total_prods28p revenue_r_m_mean revenue_r_m_p25 revenue_r_m_median revenue_r_m_p75 revenue_r_m_p90 revenue_r_m_p95 products_r_m_mean products_r_m_p25 products_r_m_median products_r_m_p75 products_r_m_p90 products_r_m_p95 groups_r_m_mean groups_r_m_p25 groups_r_m_median groups_r_m_p75 groups_r_m_p90 groups_r_m_p95 quality0_m_mean quality0_m_p25 quality0_m_median quality0_m_p75 quality0_m_p90 quality0_m_p95, by(censored_gr) statistics(mean) columns(statistics) listwise nototal
eststo firm2_v2

esttab firm2_v1 firm1_v1 using "OUTPUT/Table2.tex", cell(mean(fmt(%9.0fc %9.1gc))) collabels(none) refcat(maxAge_i "\\Duration (quarters)" revenue_r_m_mean "\\Revenue (quarterly, \\$1,000)" products_r_m_mean "\\Products (quarterly)" groups_r_m_mean "\\Groups (quarterly)", nolabel) nostar nonotes nonumbers unstack noobs not label replace

